<?php


namespace UU\Suite\DingTalk;


use UU\Suite\Kernel\Contracts\HttpClientInterface;

class User
{
    /**
     * @var HttpClientInterface
     */
    private $HttpClient;

    public function __construct(HttpClientInterface $HttpClient)
    {
        $this->HttpClient = $HttpClient;
    }

    public function list($data): array
    {
        $data = $this->HttpClient->post('/topapi/v2/user/list', [
            'json' => $data
        ]);
        return $this->result($data);
    }

    public function sub_id_list($dept_id, string $language = 'zh_CN'): array
    {
        $data = $this->HttpClient->post('/topapi/v2/department/listsubid', ['json' => compact('dept_id', 'language')]);
        return $this->result($data);
    }

    public function create($data): array
    {
        // TODO: Implement create() method.
    }

    public function update($data): array
    {
        // TODO: Implement update() method.
    }

    public function delete(int $dept_id): array
    {
        // TODO: Implement delete() method.
    }


    public function result($data)
    {
        if (isset($data['errcode']) && $data['errcode'] == 0) {
            return $data['result'] ?? [];
        }
        throw new \Exception($data['sub_msg'] ?? '请求错误',$data['errcode'] ?? 0);
    }

    public function get( $userid): array
    {
        $data = $this->HttpClient->post('/topapi/v2/user/get', ['json' => compact('userid')]);
        return $this->result($data);
    }
}